package com.fh.common.interceptors;/**
 * 描述:
 * Author:袁亚强
 * Date:2021/12/2014:31
 */

import com.fh.common.exceptions.ParamException;
import com.fh.common.exceptions.TokenException;
import com.fh.user.entity.vo.User;
import com.fh.utils.JWT;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * created by macro on 2021/12/20,
 */
@Component
public class OutherInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        /* 认证的逻辑    1  判断是否有 token   2 判断token是否正确  3 token解析后  */
        String token = request.getHeader("token");

        if (StringUtils.isEmpty(token)) {
            throw new TokenException("缺少token");
        }
        User unsign = JWT.unsign(token, User.class);
        if (unsign == null) {
            throw new TokenException("没有登录");
        }

        request.setAttribute("u", unsign);

        return true;
    }
}
